package com.mifengs.mall.ui.login;

import android.widget.Toast;

import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.commonbase.util.ToastUitl;
import com.mifengs.mall.event.LoginEvent;
import com.mifengs.mall.netWork.NetWork;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.HashMap;

import okhttp3.ResponseBody;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class RetrievePasswordPresent implements RetrievePasswordContract.Presenter {
    private final RetrievePasswordContract.View view;

    public RetrievePasswordPresent(RetrievePasswordContract.View view) {
        this.view = view;
    }


    /**
     * 获取后台发送的验证码
     */
    @Override
    public void sendVerificationCode() {
        String phone = view.getPhone();
        String randNum = view.getPhotoCode();
        HashMap<String, String> paraMaps = new HashMap<String, String>();
        paraMaps.put("randNum", randNum);
        paraMaps.put("mobile", phone);
        view.showLoading();
        NetWork.getFindPWApi().getVerificationCodeRepairPw(paraMaps).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onCompleted() {
                        view.hideLoading();
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        view.hideLoading();
                        view.showNetError();
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        view.hideLoading();

                        try {
                            String result = responseBody.string();
                            JSONObject object = new JSONObject(result);
                            String status = object.getString("status");
                            String info = object.getString("info");
                            if ("0000".equals(status)) {
                                ToastUitl.show("验证码发送成功，请注意查收短信", Toast.LENGTH_LONG);
                                view.smsSuccess();
                            } else {
                                ToastUitl.show(info, Toast.LENGTH_LONG);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    /**
     * 提交找回密码信息
     */
    @Override
    public void retrievePW() {
        String phone = view.getPhone();
        String code = view.getVerificationCode();
        String newPW = view.getNewPasswodrd();
        String surePW = view.getSurePassword();
        HashMap<String, Object> paraMaps = new HashMap<>();
        paraMaps.put("mobile", phone);
        paraMaps.put("mobileCode", code);
        paraMaps.put("newPassword", newPW);
        paraMaps.put("usertoken", PreferenceUtils.getToken());
        paraMaps.put("mobiletype", 1);
        view.showLoading();
        NetWork.getFindPWApi()
                .findBackPW(paraMaps)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(view.<ResponseBody>bindToLife())
                .subscribe(new Observer<ResponseBody>() {
                    @Override
                    public void onCompleted() {
                        view.hideLoading();
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        view.hideLoading();
                        view.showNetError();
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        view.hideLoading();
                        try {
                            String result = responseBody.string();
                            JSONObject object = new JSONObject(result);
                            String status = object.getString("status");
                            String info = object.getString("info");
                            if ("0000".equals(status)) {
                                JSONObject jsonObject = object.getJSONObject("paramer");
                                String token = jsonObject.getString("loginToken");
                                PreferenceUtils.saveToken(token);
                                EventBus.getDefault().post(new LoginEvent(LoginEvent.LOGIN_SUCCESS));
                                view.finish();
                            } else  {
                                ToastUitl.show(info, Toast.LENGTH_LONG);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
